package com.tensquare.friend.controller;

import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/like/{friendId}/{type}", method = RequestMethod.PUT)
    public Result like(@PathVariable String friendId, @PathVariable String type) {
        //获取JwtToken的载荷部分
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        //判断载荷是否为空
        if (claims_user == null) {
            return new Result(true, StatusCode.LOGINERROR,"请先登录");
        }
        //获取当前登录的用户id
        String userId = claims_user.getId();
        if ("1".equals(type)) { //喜欢
            friendService.addFriend(userId,friendId);
        } else {  //不喜欢
            friendService.addNoFriend(userId,friendId);
        }

        return new Result(true, StatusCode.OK,"添加成功");


    }


    @RequestMapping(value = "/{friendId}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String friendId) {
        //获取JwtToken的载荷部分
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        //判断载荷是否为空
        if (claims_user == null) {
            throw new RuntimeException("请先登录");
        }
        //获取当前登录的用户id
        String userId = claims_user.getId();
        //删除好友
        friendService.delFriend(userId,friendId);
        return new Result(true, StatusCode.OK,"删除成功");


    }
}
